package com.atguigu.bigdata.scala.mytest.chapter09

object Test_Exception {
  def main(args: Array[String]): Unit = {
    val a:Int =1;
    val b:Int =0;
    try {
      val i: Int = a / b //ArithmeticException异常
      val user:User=null
      user.test()//NullPointerException异常
    } catch {
      case a:ArithmeticException => println("除数为0"+a)//匹配ArithmeticException异常
      case b:NullPointerException => println("空指针"+b)//匹配NullPointerException异常
      case _  => println("其他异常")//匹配其他所有异常
    }
  }

  @throws[Exception]//用于提示调用该方法时会产生的异常，调用时要解决异常或抛出
  def test(): Unit ={
    throw new ArithmeticException("自己抛出的异常")
  }
}

class User{
  def test(): Unit ={
    print("1111111")
  }
}

